{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import requests_html\n",
    "from requests_html import HTMLSession\n",
    "import urllib.parse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://movie.douban.com/tag/#/?sort=S&range=0,10&tags=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E9%99%86,%E7%94%B5%E5%BD%B1\"\n",
    "session = HTMLSession()\n",
    "r = session.get( url )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "dict_xpaths={ \n",
    "    '名称': './/div[contains(@class,\"info\")]/a/text()', \n",
    "    '评分': './/div[contains(@class,\"info\")]/div/span/span[2]', \n",
    "    '导演': './/div[contains(@class,\"info\")]/p/span[1]', \n",
    "    '主演': './/div[contains(@class,\"info\")]/p/span[2]',\n",
    "}\n",
    "\n",
    "# 先取特定元素, 精准打击其子后辈\n",
    "\n",
    "主要元素 = r.html.xpath( \\\n",
    "    './/div[@class=\"article\"]/div[@class=\"list-wp\"]')\n",
    "\n",
    "# 只对主要元素下进行.xpath取值\n",
    "print(主要元素)\n",
    "数据字典 = {k:r.html.xpath(v) for k,v in dict_xpaths.items()}\n",
    "\n",
    "print([len(v) for k,v in 数据字典.items()])\n",
    "\n",
    "df = pd.DataFrame.from_dict(数据字典, orient='index')\n",
    "print(df)\n",
    "\n",
    "数据 = pd.DataFrame(数据字典)\n",
    "display(数据)\n",
    "# 数据.iloc[:,[0,1,2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame.from_dict(d, orient='index')\n",
    "print(df)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
